import 'dart:io';

import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:ytbusiness/config/style.dart';
import 'package:ytbusiness/page/repair_factory/accessories_query/repairs_inquiry.dart';
import 'package:ytbusiness/utils/helper.dart';
import 'package:ytbusiness/utils/screen_adaptation.dart';
import 'package:ytbusiness/widgets/widgets.dart';
class CarLicense extends StatefulWidget {
  @override
  _CarLicenseState createState() => _CarLicenseState();
}

class _CarLicenseState extends State<CarLicense> with AutomaticKeepAliveClientMixin{
  File image;
  @override
  Widget build(BuildContext context) {
    super.build(context);
    return SingleChildScrollView(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          GestureDetector(
            onTap: (){
              _customBottomSheet();
            },
            child: Container(
              margin: EdgeInsets.only(
                  left: setWidth(15), right: setWidth(15), top: setWidth(21)),
              width: setWidth(298),
              height: setWidth(199),
              alignment: Alignment.center,
              child: image == null?Image.asset("assets/images/car_number.png",fit: BoxFit.cover):Image.file(image,fit: BoxFit.cover),
            ),
          ),
          GestureDetector(
            onTap: () {
              jump(context,widget: RepairsInquiry(),name: "repairs_inquiry");
            },
            child: Container(
              margin: EdgeInsets.only(
                  left: setWidth(15),
                  right: setWidth(15),
                  top: setWidth(54)),
              decoration: BoxDecoration(
                  color: Color(0xff00C65F).withOpacity(0.6),
                  borderRadius: BorderRadius.circular(3)),
              height: setWidth(44),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text(
                    "确定",
                    style: TextStyle(
                        color: Colors.white,
                        fontSize: setSp(15),
                        fontWeight: FontWeight.w600),
                  ),
                ],
              ),
            ),
          )
        ],
      ),
    );
  }

  ///自定义底部弹窗
  void _customBottomSheet() {
    showModalBottomSheet(
        backgroundColor: Colors.white,
        context: context,
        builder: (BuildContext context) {
          return Container(
            child: SingleChildScrollView(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  GestureDetector(
                    onTap: () {
                      ///去拍照
                      _checkImage(1);
                      Navigator.of(context).pop();
                    },
                    child: Container(
                        height: setWidth(52),
                        decoration: BoxDecoration(
                          color: Colors.white,
                        ),
                        child: Center(
                            child: simpleText("拍照", color: YTStyle.textColor))),
                  ),
                  Container(
                    height: setWidth(0.5),
                    color: Color(0xffDDDDDD),
                  ),
                  GestureDetector(
                    onTap: () {
                      ///相册选择
                      _checkImage(0);
                      Navigator.of(context).pop();
                    },
                    child: Container(
                        height: setWidth(52),
                        decoration: BoxDecoration(
                          color: Colors.white,
                        ),
                        child: Center(
                          child: simpleText("相册", color: YTStyle.textColor),
                        )),
                  ),
                  Container(
                    height: setWidth(0.5),
                    color: Color(0xffDDDDDD),
                  ),
                  Container(
                    height: setWidth(10),
                    color: Color(0xffF0F0F0),
                  ),
                  GestureDetector(
                    onTap: () => Navigator.of(context).pop(),
                    child: Container(
                        height: setWidth(52),
                        decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.circular(3.0),
                        ),
                        child: Center(
                          child: simpleText("取消", color: YTStyle.textColor),
                        )),
                  )
                ],
              ),
            ),
          );
        });
  }

  ///
  _checkImage(int type) async {
    if (type == 0) {
      await ImagePicker.pickImage(
        source: ImageSource.gallery,
      ).then((value) {
        if (value != null) {
          setState(() {
            image = value;
          });
        }
      });
    } else {
      await ImagePicker.pickImage(
        source: ImageSource.camera,
      ).then((value) {
        if (value != null) {
          setState(() {
            image = value;
          });
        }
      });
    }
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive  => true;
}
